<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.searchKey"
        placeholder="请输入关键词"
        style="width: 200px;margin-right: 20px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
    <!-- <el-input v-model="Pages.readName" placeholder="开始时间"> </el-input> -->
        <el-date-picker
                v-model="listQuery.startTime"
                type="datetime"
                placeholder="开始时间">
        </el-date-picker>

        <el-date-picker
            v-model="listQuery.endTime"
            type="datetime"
            placeholder="结束时间">
        </el-date-picker>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-refresh"
        @click="handleFilter"
      >
        刷新
      </el-button>
      <el-button
        v-has="'wxUniClassTextList:add'"
        v-waves
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        {{ $t('table.add') }}
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
    <!-- 前面的勾选按钮 -->
     <el-table-column
      type="selection"
      width="55"
      header-align="center"
      align="center"
      >
    </el-table-column>

    <el-table-column
      prop="id"
      label="序号"
      width="140"
      align="center"
      header-align="center"
     >
    </el-table-column>

	<!-- 排除ID主键 -->
      <el-table-column :label="tempDesc.dircId" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.dircId }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.title" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.content" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.status" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.isFree" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.isFree }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.salary" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.salary }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.reduceSalary" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.reduceSalary }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.createUser" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createUser }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.createTime" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.updateTime" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>



      <!-- 这里是操作按钮，和权限相关 -->
      <el-table-column v-if="checkBtnPermission(['wxUniClassTextList:edit','wxUniClassTextList:delete'])" :label="$t('table.actions')" align="center" width="310" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-has="'wxUniClassTextList:edit'" type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-has="'wxUniClassTextList:delete'" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">
            {{ $t('table.delete') }}
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 編輯窗口 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :before-close="handleClose" width="80%" @open="initDialog">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item :label="tempDesc.dircId" prop="dircId">
          <el-input v-model="temp.dircId" :placeholder="'请输入' + tempDesc.dircId" />
        </el-form-item>
        <el-form-item :label="tempDesc.title" prop="title">
          <el-input v-model="temp.title" :placeholder="'请输入' + tempDesc.title" />
        </el-form-item>
        <el-form-item :label="tempDesc.content" prop="content">
          <el-input v-model="temp.content" :placeholder="'请输入' + tempDesc.content" />
        </el-form-item>
        <el-form-item :label="tempDesc.status" prop="status">
          <el-input v-model="temp.status" :placeholder="'请输入' + tempDesc.status" />
        </el-form-item>
        <el-form-item :label="tempDesc.isFree" prop="isFree">
          <el-input v-model="temp.isFree" :placeholder="'请输入' + tempDesc.isFree" />
        </el-form-item>
        <el-form-item :label="tempDesc.salary" prop="salary">
          <el-input v-model="temp.salary" :placeholder="'请输入' + tempDesc.salary" />
        </el-form-item>
        <el-form-item :label="tempDesc.reduceSalary" prop="reduceSalary">
          <el-input v-model="temp.reduceSalary" :placeholder="'请输入' + tempDesc.reduceSalary" />
        </el-form-item>
        <el-form-item :label="tempDesc.createUser" prop="createUser">
          <el-input v-model="temp.createUser" :placeholder="'请输入' + tempDesc.createUser" />
        </el-form-item>

      </el-form>

      <tinymce :height="300" v-model="content" id='tinydemo'></tinymce>

     <!-- <div class="editor-content" v-html="content" /> -->

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { checkBtnPermission } from '@/utils/permission' // 权限判断函数
import { fetchList, createRow, updateRow, deleteRow, generateQRUrl } from '@/api/wx-uniapp/uni-class-text'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import dateToString from '@/utils/DateTimeToString'
import Tinymce from '@/components/Tinymce'

// 常量title
const tempDesc = {
  	'id': '主鍵',
  	'dircId': '关联到字典表',
  	'title': '标题',
  	'content': '课程内容',
  	'status': '状态:0未上线/撤销, 1上线 ',
  	'isFree': '是否免费',
  	'salary': '当前金额',
  	'reduceSalary': '已优惠金额',
  	'createUser': '创建人',
  	'createTime': '创建时间',
  	'updateTime': '更新时间',

}
export default {
  // name: 'ComplexTable',
  components: { Pagination,Tinymce },
  directives: { waves },
  filters: {},
  data() {
    return {
      content: '',//输入的html富文本内容
      tinymceHtml: '请输入内容',

      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        searchKey: '',
        startTime: '',
        endTime: '',
      },
      showReviewer: false,
      temp: {
  	     'id': '',
  	     'dircId': '',
  	     'title': '',
  	     'content': '',
  	     'status': '',
  	     'isFree': '',
  	     'salary': '',
  	     'reduceSalary': '',
  	     'createUser': '',
  	     'createTime': '',
  	     'updateTime': '',
      },
      tempDesc: tempDesc,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '增加'
      },
      rules: {
        dircId: [{ required: true, message: '请输入' + tempDesc.dircId, trigger: 'blur' }],
        title: [{ required: true, message: '请输入' + tempDesc.title, trigger: 'blur' }],
        content: [{ required: true, message: '请输入' + tempDesc.content, trigger: 'blur' }],
        status: [{ required: true, message: '请输入' + tempDesc.status, trigger: 'blur' }],
        isFree: [{ required: true, message: '请输入' + tempDesc.isFree, trigger: 'blur' }],
        salary: [{ required: true, message: '请输入' + tempDesc.salary, trigger: 'blur' }],
        reduceSalary: [{ required: true, message: '请输入' + tempDesc.reduceSalary, trigger: 'blur' }],
        createUser: [{ required: true, message: '请输入' + tempDesc.createUser, trigger: 'blur' }],
      },

    }
  },

  created() {
    this.getList();

  },
  methods: {
    checkBtnPermission,
    getList() {
      this.listLoading = true
      //判断时间是否为空
      this.listQuery.startTime = dateToString(this.listQuery.startTime);
      this.listQuery.endTime = dateToString(this.listQuery.endTime);
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
  	     'id': '',
  	     'dircId': '',
  	     'title': '',
  	     'content': '',
  	     'status': '',
  	     'isFree': '',
  	     'salary': '',
  	     'reduceSalary': '',
  	     'createUser': '',
  	     'createTime': '',
  	     'updateTime': '',
        //'id': '',
        //'name': '',
        //'account': '',
        //'appid': '',
        //'appsecret': '',
        //'url': '',
        //'baseAPI': '',
        //'token': '',
        //'aeskey': ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick( () => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createRow(this.temp).then( () => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateRow(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var params = { 'id': row.id }
          deleteRow(params).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
    },
    generateQRUrl(row) {
      this.$confirm('确定生成二维码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const tempData = Object.assign({}, row)
        generateQRUrl(tempData).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '生成二维码成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    //提示关闭编辑窗口
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    initDialog(){ //初始化窗口
      tinymce.init({
          selector: '#tinydemo', //容器，可使用css选择器
          language:'zh_CN', //调用放在langs文件夹内的语言包
          toolbar: false, //隐藏工具栏
          menubar: false, //隐藏菜单栏
          inline: true, //开启内联模式
          plugins: [ 'quickbars','link','table','paste' ], //选择需加载的插件
          paste_data_images: true,
          //选中时出现的快捷工具，与插件有依赖关系
          quickbars_selection_toolbar: 'bold italic forecolor | link blockquote quickimage',
      });
    },
  }

}
</script>
